from ftplib import FTP
import pymysql
import paramiko



def getboplist(get_ip,path_name):
    conn = pymysql.connect(host='10.20.27.20', user='root', passwd='Dar960207!@#', db='dar')
    cur = conn.cursor()
    sql1='select path_ip,path_port,get_path,path_user,path_password from updatepath where server_ip ="{}" and path_name="{}"'.format(get_ip,path_name)
    num = cur.execute(sql1)
    data = cur.fetchall()

    ftp = FTP()
    ftp.set_debuglevel(2)
    ftp.connect(data[0][0], data[0][1])
    ftp.login(data[0][3], data[0][4])
    ftp.cwd(data[0][2].encode("UTF-8").decode("latin-1"))
    # 显示文件名称
    lists = ftp.nlst()
    lists.reverse()
    list_str = ''
    for x in lists:
        list_str = list_str + ' ' + x
    ftp.set_debuglevel(0)
    ftp.quit()
    cur.close()
    conn.close()
    return list_str

def getbopwar(get_ip,version,path_name):
    conn = pymysql.connect(host='10.20.27.20', user='root', passwd='Dar960207!@#', db='dar')
    cur = conn.cursor()
    sql1='select path_ip,path_port,get_path,path_user,path_password from updatepath where server_ip ="{}" and path_name="{}"'.format(get_ip,path_name)
    num = cur.execute(sql1)
    data = cur.fetchall()

    print()
    ftp = FTP()
    ftp.set_debuglevel(2)
    ftp.connect(data[0][0], data[0][1])
    ftp.login(data[0][3], data[0][4])
    print((data[0][2]+version).encode("UTF-8").decode("latin-1"))
    ftp.cwd((data[0][2]+version).encode("UTF-8").decode("latin-1"))
    # 显示文件名称
    lists = ftp.nlst()
    lists.reverse()
    list_str = ''
    for x in lists:
        if lists.index(x)== 0 :
            list_str=list_str+x
        else:
            list_str = list_str + ' ' + x
    ftp.set_debuglevel(0)
    ftp.quit()
    cur.close()
    conn.close()
    return list_str

def getmember(ip):
    #conn = pymysql.connect(host='localhost', user='root', passwd='Dar960207!@#', db='dar')
    conn = pymysql.connect(host='10.20.27.20', user='root', passwd='Dar960207!@#', db='dar')
    cur = conn.cursor()
    sql1 = 'select members from product where server_address ="{}"'.format(ip)
    num = cur.execute(sql1)
    data = cur.fetchall()
    member_str = ''
    sql2 = 'select username from user'
    num1 = cur.execute(sql2)
    data1 = cur.fetchall()
    for x in data1:

        if x[0] not in data[0][0]:
            if len(member_str)==0:
                member_str=member_str+x[0]
            else:
                member_str = member_str+' '+x[0]
    return member_str

def jar_version(ip,port,username,password,path,war):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ip, port=port, username=username, password=password)
    stdin, stdout, stderr = ssh.exec_command('cd /home; python test.py {} {}'.format(path, war))
    darre = str(stdout.read()).replace('b"', '').replace('"', '').replace('\\n', '')
    di = eval(darre)
    ssh.close()
    return di

# def get_tomcat_unique(ip,path_name):
#     conn = pymysql.connect(host='10.20.27.20', user='root', passwd='Dar960207!@#', db='dar')
#     cur = conn.cursor()
#     sql = 'select tomcat_unique from updatepath a where a.path_name ="{}" and a.server_ip = "{}"'.format(path_name, ip)
#     num = cur.execute(sql)
#     data = cur.fetchall()
#     if len(data) != 0:
#         tomcat_unique = data[0][0]
#         return tomcat_unique